package com.spark.lola.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.spark.lola.feign.GdsFeignService;
import com.spark.lola.feign.LogisticsFeignService;
import com.spark.lola.feign.SupFeignService;
import com.spark.lola.logistics.entity.LocationInfo;
import com.spark.lola.mapper.statistics.StatisticsProductMapper;
import com.spark.lola.mapper.statistics.StatisticsPurchaseMapper;
import com.spark.lola.mapper.statistics.StatisticsSalesmanMapper;
import com.spark.lola.mapper.statistics.StatisticsSupplierMapper;
import com.spark.lola.statistics.dto.DealExpensesChange;
import com.spark.lola.statistics.dto.DealLogisticsChange;
import com.spark.lola.statistics.entity.StatisticsProduct;
import com.spark.lola.statistics.entity.StatisticsPurchase;
import com.spark.lola.statistics.entity.StatisticsSalesman;
import com.spark.lola.statistics.entity.StatisticsSupplier;
import com.spark.lola.mapper.statistics.*;
import com.spark.lola.statistics.entity.*;
import com.spark.lola.support.common.util.TimeUtil;
import com.spark.lola.support.wms.constant.KafkaTopic;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @Author: DengYeJun
 * @Date: 2020/5/12 14:00
 */
@Component
public class StatisticsConsumer {
    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private GdsFeignService gdsFeignService;
    @Autowired
    private SupFeignService supFeignService;
    @Autowired
    private LogisticsFeignService logisticsFeignService;
    @Autowired
    private StatisticsProductMapper productMapper;
    @Autowired
    private StatisticsSupplierMapper supplierMapper;
    @Autowired
    private StatisticsPurchaseMapper purchaseMapper;
    @Autowired
    private StatisticsDestinationMapper destinationMapper;

    @Autowired
    private StatisticsLogisticsMapper statisticsLogisticsMapper;

    @Autowired
    private StatisticsDriverMapper statisticsDriverMapper;

    @Autowired
    private StatisticsCarMapper statisticsCarMapper;

    @Autowired
    private StatisticsSalesmanMapper salesmanMapper;
    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    /**
     * 货品销售统计
     *
     * @param consumerRecord
     */
    @KafkaListener(topics = KafkaTopic.STATISTICS_CAL_PRODCUT)
    @Transactional(rollbackFor = Exception.class)
    public void calProduct(ConsumerRecord<String, String> consumerRecord) {
        logger.info("===consumer【STATISTICS_CAL_PRODCUT】货品【{}】日期【{}】===START===", consumerRecord.key(), consumerRecord.value());
        Long productId = Long.valueOf(consumerRecord.key());
        Date day = TimeUtil.parse(consumerRecord.value());
        String createDate = new SimpleDateFormat("yyyy-MM-dd").format(day);
        // 获取指定日期统计记录
        StatisticsProduct model = productMapper.findOne(productId, createDate);
        if (model == null) {
            logger.warn("===统计记录不存在");
            model = new StatisticsProduct(productId, day);
        }
        // 获取货品统计数据
        Map<String, BigDecimal> statistics = gdsFeignService.statistics(
                productId,
                TimeUtil.getStartOfDay(day),
                TimeUtil.getEndOfDay(day)
        );
        model.setSales(statistics.get("settleMoney"));
        model.setWeight(statistics.get("settleQuantity"));
        model.setTransportWeight(statistics.get("transportWeight"));

        logger.info("===保存||更新记录...");
        if (model.getId() == null) {
            if (productMapper.insert(model) == 0) {
                logger.error("记录修改失败");
            }
        } else {
            if (productMapper.updateById(model) == 0) {
                logger.error("记录修改失败");
            }
        }
        logger.info("===consumer【STATISTICS_CAL_PRODCUT】货品【{}】===END===", consumerRecord.key());
    }

    /**
     * 供应商销售统计
     *
     * @param consumerRecord
     */
    @KafkaListener(topics = KafkaTopic.STATISTICS_CAL_SUPPLIER)
    @Transactional(rollbackFor = Exception.class)
    public void calSupplier(ConsumerRecord<String, String> consumerRecord) {
        logger.info("===consumer【STATISTICS_CAL_SUPPLIER】供应商【{}】日期【{}】===START===", consumerRecord.key(), consumerRecord.value());
        Long supplierId = Long.valueOf(consumerRecord.key());
        Date day = TimeUtil.parse(consumerRecord.value());
        String createDate = new SimpleDateFormat("yyyy-MM-dd").format(day);
        // 获取指定日期统计记录
        StatisticsSupplier model = supplierMapper.findOne(supplierId, createDate);
        if (model == null) {
            logger.warn("===统计记录不存在");
            model = new StatisticsSupplier(supplierId, day);
        }
        // 获取供应商统计数据
        Map<String, BigDecimal> statistics = supFeignService.statistics(
                supplierId, null, null,
                TimeUtil.getStartOfDay(day),
                TimeUtil.getEndOfDay(day)
        );
        logger.info("===【{}】", JSON.toJSONString(statistics));
        model.setOrderCount(statistics.get("orderCount").intValue());
        model.setOrderSales(statistics.get("actualAmount"));
        model.setOrderWeight(statistics.get("settleQuantity"));
        model.setOrderPayment(statistics.get("orderPayment"));
        model.setReturnCount(statistics.get("returnCount").intValue());
        logger.info("保存||更新记录...");
        if (model.getId() == null) {
            if (supplierMapper.insert(model) == 0) {
                logger.error("记录修改失败");
            }
        } else {
            if (supplierMapper.updateById(model) == 0) {
                logger.error("记录修改失败");
            }
        }
        TransactionSynchronizationManager.registerSynchronization(
                new TransactionSynchronizationAdapter() {
                    @Override
                    public void afterCommit() {
                        // kafka推送
                        kafkaTemplate.send(KafkaTopic.STATISTICS_CAL_PURCHASE, consumerRecord.value(), null);
                    }
                });
        logger.info("===consumer【STATISTICS_CAL_SUPPLIER】供应商【{}】日期【{}】===END===", consumerRecord.key(), consumerRecord.value());
    }


    /**
     * 纸厂销售统计
     *
     * @param consumerRecord
     */
    @KafkaListener(topics = KafkaTopic.STATISTICS_CAL_DESTINATION)
    @Transactional(rollbackFor = Exception.class)
    public void calDestination(ConsumerRecord<String, String> consumerRecord) {
        logger.info("===consumer【STATISTICS_CAL_DESTINATION】纸厂【{}】日期【{}】===START===", consumerRecord.key(), consumerRecord.value());
        Long parentId = Long.valueOf(consumerRecord.key());
        Date day = TimeUtil.parse(consumerRecord.value());
        String createDate = new SimpleDateFormat("yyyy-MM-dd").format(day);
        // 获取指定日期统计记录
        StatisticsDestination model = destinationMapper.findOne(parentId, createDate);
        if (model == null) {
            logger.warn("===统计记录不存在");
            LocationInfo locationInfo = logisticsFeignService.getCompanyById(parentId);
            model = new StatisticsDestination(parentId, locationInfo.getName(), day);
        }
        // 获取供应商统计数据
        Map<String, BigDecimal> statistics = supFeignService.statistics(
                null, null, parentId,
                TimeUtil.getStartOfDay(day),
                TimeUtil.getEndOfDay(day)
        );
        model.setOrderCount(statistics.get("orderCount").intValue());
        model.setOrderSales(statistics.get("actualAmount"));
        model.setOrderWeight(statistics.get("settleQuantity"));
        model.setOrderPayment(statistics.get("orderPayment"));
        model.setReturnCount(statistics.get("returnCount").intValue());
        logger.info("保存||更新记录...");
        if (model.getId() == null) {
            if (destinationMapper.insert(model) == 0) {
                logger.error("记录修改失败");
            }
        } else {
            if (destinationMapper.updateById(model) == 0) {
                logger.error("记录修改失败");
            }
        }
        logger.info("===consumer【STATISTICS_CAL_DESTINATION】纸厂【{}】日期【{}】===END===", consumerRecord.key(), consumerRecord.value());
    }

    /**
     * 业务员 销售统计
     *
     * @param consumerRecord
     */
    @KafkaListener(topics = KafkaTopic.STATISTICS_CAL_SALESMAN)
    @Transactional(rollbackFor = Exception.class)
    public void calSalesman(ConsumerRecord<String, String> consumerRecord) {
        logger.info("===consumer【STATISTICS_CAL_SALESMAN】业务员【{}】日期【{}】===START===", consumerRecord.key(), consumerRecord.value());
        Long salesmanId = Long.valueOf(consumerRecord.key());
        Date day = TimeUtil.parse(consumerRecord.value());
        String createDate = new SimpleDateFormat("yyyy-MM-dd").format(day);
        // 获取指定日期统计记录
        StatisticsSalesman model = salesmanMapper.findOne(salesmanId, createDate);
        if (model == null) {
            logger.warn("===统计记录不存在");
            model = new StatisticsSalesman(salesmanId, day);
        }
        // 获取业务员统计数据
        Map<String, BigDecimal> statistics = supFeignService.statistics(
                null,
                salesmanId, null,
                TimeUtil.getStartOfDay(day),
                TimeUtil.getEndOfDay(day)
        );
        model.setOrderCount(statistics.get("orderCount").intValue());
        model.setOrderSales(statistics.get("actualAmount"));
        model.setOrderWeight(statistics.get("settleQuantity"));
        model.setOrderPayment(statistics.get("orderPayment"));
        model.setReturnCount(statistics.get("returnCount").intValue());
        logger.info("保存||更新记录...");
        if (model.getId() == null) {
            if (salesmanMapper.insert(model) == 0) {
                logger.error("记录修改失败");
            }
        } else {
            if (salesmanMapper.updateById(model) == 0) {
                logger.error("记录修改失败");
            }
        }
        logger.info("===consumer【STATISTICS_CAL_SALESMAN】业务员【{}】日期【{}】===END===", consumerRecord.key(), consumerRecord.value());
    }

    @KafkaListener(topics = KafkaTopic.STATISTICS_CAL_PURCHASE)
    @Transactional(rollbackFor = Exception.class)
    public void calPurchase(ConsumerRecord<String, String> consumerRecord) {
        logger.info("===consumer【STATISTICS_CAL_PURCHASE】采购报表 日期【{}】===START===", consumerRecord.key());
        Date day = new Date();
        try {
            day = TimeUtil.parse(consumerRecord.key());
        }
        catch (Exception e){
            logger.error("时间转化失败【{}】{}",day,e.getMessage());
            return;
        }
        String createDate = new SimpleDateFormat("yyyy-MM-dd").format(day);
        // 获取指定日期统计记录
        StatisticsPurchase model = purchaseMapper.findOne(createDate);
        if (model == null) {
            logger.warn("===统计记录不存在");
            model = new StatisticsPurchase(day);
        }
        // 获取采购统计数据
        StatisticsSupplier statistics = supplierMapper.statistics(0L,
                TimeUtil.getStartOfDay(day),
                TimeUtil.getEndOfDay(day)
        );

        model.setOrderCount(statistics.getOrderCount());
        model.setOrderSales(statistics.getOrderSales());
        model.setOrderPayment(statistics.getOrderPayment());
        model.setOrderWeight(statistics.getOrderWeight());
        model.setReturnCount(statistics.getReturnCount());
        logger.info("保存||更新记录...");
        if (model.getId() == null) {
            if (purchaseMapper.insert(model) == 0) {
                logger.error("记录修改失败");
            }
        } else {
            if (purchaseMapper.updateById(model) == 0) {
                logger.error("记录修改失败");
            }
        }
        logger.info("===consumer【STATISTICS_CAL_PURCHASE】采购报表 日期【{}】===END===", consumerRecord.key());
    }

    @KafkaListener(topics = KafkaTopic.STATISTICS_LOGISTICS_CHANGE)
    @Transactional(rollbackFor = Exception.class)
    public void logisticsRejected(ConsumerRecord<String, String> consumerRecord) throws Exception {
        DealLogisticsChange dealStatisticsChange = JSONObject.parseObject(consumerRecord.value(), DealLogisticsChange.class);
        // 格式化时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date createDate = simpleDateFormat.parse(simpleDateFormat.format(dealStatisticsChange.getCreateDate()));
        Date newDate = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
        logger.info("===consumer【STATISTICS_LOGISTICS_REJECTED】货运报表 日期【{}】===START===", createDate);

        logger.info("===更新货运总报表（货运单部分） ===START===");
        StatisticsLogistics sl = statisticsLogisticsMapper.findByCreateDate(createDate);
        switch (dealStatisticsChange.getType()) {
            case "pricing":
                if (sl == null) {
                    sl = new StatisticsLogistics(newDate);
                }
                sl.setLogisticsNumber(sl.getLogisticsNumber() + 1);
                sl.setLogisticsMoney(sl.getLogisticsMoney().add(dealStatisticsChange.getLogisticsMoney()));
                sl.setLogisticsNotSettlementMoney(sl.getLogisticsNotSettlementMoney().add(dealStatisticsChange.getLogisticsMoney()));
                sl.setLogisticsTotalWeight(sl.getLogisticsTotalWeight().add(dealStatisticsChange.getLogisticsTotalWeight()));
                break;
            case "complete":
                if (sl == null) {
                    sl = new StatisticsLogistics(newDate);
                    sl.setLogisticsNumber(sl.getLogisticsNumber() + 1);
                    sl.setLogisticsMoney(sl.getLogisticsMoney().add(dealStatisticsChange.getLogisticsMoney()));
                    sl.setLogisticsTotalWeight(sl.getLogisticsTotalWeight().add(dealStatisticsChange.getLogisticsTotalWeight()));
                } else {
                    sl.setLogisticsNotSettlementMoney(sl.getLogisticsNotSettlementMoney().subtract(dealStatisticsChange.getLogisticsMoney()));
                }
                sl.setLogisticsSettlementMoney(sl.getLogisticsSettlementMoney().add(dealStatisticsChange.getLogisticsMoney()));
                break;
            case "rejected":
                if (sl != null) {
                    sl.setLogisticsNumber(sl.getLogisticsNumber() - 1);
                    sl.setLogisticsMoney(sl.getLogisticsMoney().subtract(dealStatisticsChange.getLogisticsMoney()));
                    sl.setLogisticsNotSettlementMoney(sl.getLogisticsNotSettlementMoney().subtract(dealStatisticsChange.getLogisticsMoney()));
                    sl.setLogisticsTotalWeight(sl.getLogisticsTotalWeight().subtract(dealStatisticsChange.getLogisticsTotalWeight()));
                }
                break;
        }
        if (sl != null && sl.getId() == null) {
            if (statisticsLogisticsMapper.insert(sl) == 0) {
                logger.error("===添加货运总报表失败");
            }
        } else {
            if (statisticsLogisticsMapper.updateById(sl) == 0) {
                logger.error("===更新货运总报表失败");
            }
        }
        logger.info("===更新总报表 ===END===");

        logger.info("===更新驾驶员报表【driverId:{}】 ===START===", dealStatisticsChange.getDriverId());
        StatisticsDriver sd = statisticsDriverMapper.findByDriverIdAndCreateDate(dealStatisticsChange.getDriverId(), createDate);
        switch (dealStatisticsChange.getType()) {
            case "pricing":
                if (sd == null) {
                    sd = new StatisticsDriver(dealStatisticsChange.getDriverId(), newDate);
                }
                sd.setLogisticsNumber(sd.getLogisticsNumber() + 1);
                sd.setLogisticsMoney(sd.getLogisticsMoney().add(dealStatisticsChange.getLogisticsMoney()));
                sd.setLogisticsNotSettlementMoney(sd.getLogisticsNotSettlementMoney().add(dealStatisticsChange.getLogisticsMoney()));
                sd.setLogisticsTotalWeight(sd.getLogisticsTotalWeight().add(dealStatisticsChange.getLogisticsTotalWeight()));
                break;
            case "complete":
                if (sd == null) {
                    sd = new StatisticsDriver(dealStatisticsChange.getDriverId(), newDate);
                    sd.setLogisticsNumber(sd.getLogisticsNumber() + 1);
                    sd.setLogisticsMoney(sd.getLogisticsMoney().add(dealStatisticsChange.getLogisticsMoney()));
                    sd.setLogisticsTotalWeight(sd.getLogisticsTotalWeight().add(dealStatisticsChange.getLogisticsTotalWeight()));
                } else {
                    sd.setLogisticsNotSettlementMoney(sd.getLogisticsNotSettlementMoney().subtract(dealStatisticsChange.getLogisticsMoney()));
                }
                sd.setLogisticsSettlementMoney(sd.getLogisticsSettlementMoney().add(dealStatisticsChange.getLogisticsMoney()));
                break;
            case "rejected":
                if (sd != null) {
                    sd.setLogisticsNumber(sd.getLogisticsNumber() - 1);
                    sd.setLogisticsMoney(sd.getLogisticsMoney().subtract(dealStatisticsChange.getLogisticsMoney()));
                    sd.setLogisticsNotSettlementMoney(sd.getLogisticsNotSettlementMoney().subtract(dealStatisticsChange.getLogisticsMoney()));
                    sd.setLogisticsTotalWeight(sd.getLogisticsTotalWeight().subtract(dealStatisticsChange.getLogisticsTotalWeight()));
                }
                break;
        }
        if (sd != null && sd.getId() == null) {
            if (statisticsDriverMapper.insert(sd) == 0) {
                logger.error("===添加驾驶员报表失败");
            }
        } else {
            if (statisticsDriverMapper.updateById(sd) == 0) {
                logger.error("===更新驾驶员报表失败");
            }
        }
        logger.info("===更新驾驶员报表 ===END===");

        if (dealStatisticsChange.getDriverId() != 528) {
            logger.info("===更新车辆报表【carId:{}】 ===START===", dealStatisticsChange.getCarId());
            StatisticsCar sc = statisticsCarMapper.findByCarIdAndCreateDate(dealStatisticsChange.getCarId(), createDate);
            switch (dealStatisticsChange.getType()) {
                case "pricing":
                    if (sc == null) {
                        sc = new StatisticsCar(dealStatisticsChange.getCarId(), newDate);
                    }
                    sc.setLogisticsNumber(sc.getLogisticsNumber() + 1);
                    sc.setLogisticsMoney(sc.getLogisticsMoney().add(dealStatisticsChange.getLogisticsMoney()));
                    sc.setLogisticsNotSettlementMoney(sc.getLogisticsNotSettlementMoney().add(dealStatisticsChange.getLogisticsMoney()));
                    sc.setLogisticsTotalWeight(sc.getLogisticsTotalWeight().add(dealStatisticsChange.getLogisticsTotalWeight()));
                    break;
                case "complete":
                    if (sc == null) {
                        sc = new StatisticsCar(dealStatisticsChange.getCarId(), newDate);
                        sc.setLogisticsNumber(sc.getLogisticsNumber() + 1);
                        sc.setLogisticsMoney(sc.getLogisticsMoney().add(dealStatisticsChange.getLogisticsMoney()));
                        sc.setLogisticsTotalWeight(sc.getLogisticsTotalWeight().add(dealStatisticsChange.getLogisticsTotalWeight()));
                    } else {
                        sc.setLogisticsNotSettlementMoney(sc.getLogisticsNotSettlementMoney().subtract(dealStatisticsChange.getLogisticsMoney()));
                    }
                    sc.setLogisticsSettlementMoney(sc.getLogisticsSettlementMoney().add(dealStatisticsChange.getLogisticsMoney()));
                    break;
                case "rejected":
                    if (sc != null) {
                        sc.setLogisticsNumber(sc.getLogisticsNumber() - 1);
                        sc.setLogisticsMoney(sc.getLogisticsMoney().subtract(dealStatisticsChange.getLogisticsMoney()));
                        sc.setLogisticsNotSettlementMoney(sc.getLogisticsNotSettlementMoney().subtract(dealStatisticsChange.getLogisticsMoney()));
                        sc.setLogisticsTotalWeight(sc.getLogisticsTotalWeight().subtract(dealStatisticsChange.getLogisticsTotalWeight()));
                    }
                    break;
            }
            if (sc != null && sc.getId() == null) {
                if (statisticsCarMapper.insert(sc) == 0) {
                    logger.error("===添加车辆报表失败");
                }
            } else {
                if (statisticsCarMapper.updateById(sc) == 0) {
                    logger.error("===更新车辆报表失败");
                }
            }
            logger.info("===更新车辆报表 ===END===");
        }

        logger.info("===consumer【STATISTICS_LOGISTICS_REJECTED】货运报表 ===END===");
    }

    @KafkaListener(topics = KafkaTopic.STATISTICS_EXPENSES_CHANGE)
    @Transactional(rollbackFor = Exception.class)
    public void expensesRejected(ConsumerRecord<String, String> consumerRecord) throws Exception {
        DealExpensesChange dealExpensesChange = JSONObject.parseObject(consumerRecord.value(), DealExpensesChange.class);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date createDate = simpleDateFormat.parse(simpleDateFormat.format(dealExpensesChange.getCreateDate()));
        Date newDate = simpleDateFormat.parse(simpleDateFormat.format(new Date()));
        logger.info("===consumer【STATISTICS_EXPENSES_REJECTED】货运支出报表 日期【{}】===START===", createDate);

        logger.info("===更新货运总报表（支出单部分） ===START===");
        StatisticsLogistics sl = statisticsLogisticsMapper.findByCreateDate(createDate);
        switch (dealExpensesChange.getType()) {
            case "financeAudit":
                if (sl == null) {
                    sl = new StatisticsLogistics(newDate);
                }
                sl.setExpensesMoney(sl.getExpensesMoney().add(dealExpensesChange.getExpensesMoney()));
                sl.setExpensesNotSettlementMoney(sl.getExpensesNotSettlementMoney().add(dealExpensesChange.getExpensesMoney()));
                sl.setLogisticsMileage(sl.getLogisticsMileage() + dealExpensesChange.getLogisticsMileage());
                break;
            case "complete":
                if (sl == null) {
                    sl = new StatisticsLogistics(newDate);
                    sl.setExpensesMoney(sl.getExpensesMoney().add(dealExpensesChange.getExpensesMoney()));
                    sl.setLogisticsMileage(sl.getLogisticsMileage() + dealExpensesChange.getLogisticsMileage());
                } else {
                    sl.setExpensesNotSettlementMoney(sl.getExpensesNotSettlementMoney().subtract(dealExpensesChange.getExpensesMoney()));
                }
                sl.setExpensesSettlementMoney(sl.getExpensesSettlementMoney().add(dealExpensesChange.getExpensesMoney()));
                break;
            case "rejected":
                if (sl != null) {
                    sl.setExpensesMoney(sl.getExpensesMoney().subtract(dealExpensesChange.getExpensesMoney()));
                    sl.setExpensesNotSettlementMoney(sl.getExpensesNotSettlementMoney().subtract(dealExpensesChange.getExpensesMoney()));
                    sl.setLogisticsMileage(sl.getLogisticsMileage() - dealExpensesChange.getLogisticsMileage());
                }
                break;
        }
        if (sl != null && sl.getId() == null) {
            if (statisticsLogisticsMapper.insert(sl) == 0) {
                logger.error("===添加货运总报表失败");
            }
        } else {
            if (statisticsLogisticsMapper.updateById(sl) == 0) {
                logger.error("===更新货运总报表失败");
            }
        }

        logger.info("===更新总报表 ===END===");

        logger.info("===更新驾驶员报表【driverId:{}】 ===START===", dealExpensesChange.getDriverId());
        StatisticsDriver sd = statisticsDriverMapper.findByDriverIdAndCreateDate(dealExpensesChange.getDriverId(), createDate);
        switch (dealExpensesChange.getType()) {
            case "financeAudit":
                if (sd == null) {
                    sd = new StatisticsDriver(dealExpensesChange.getDriverId(), newDate);
                }
                sd.setExpensesMoney(sd.getExpensesMoney().add(dealExpensesChange.getExpensesMoney()));
                sd.setExpensesNotSettlementMoney(sd.getExpensesNotSettlementMoney().add(dealExpensesChange.getExpensesMoney()));
                sd.setLogisticsMileage(sd.getLogisticsMileage() + dealExpensesChange.getLogisticsMileage());
                break;
            case "complete":
                if (sd == null) {
                    sd = new StatisticsDriver(dealExpensesChange.getDriverId(), newDate);
                    sd.setExpensesMoney(sd.getExpensesMoney().add(dealExpensesChange.getExpensesMoney()));
                    sd.setLogisticsMileage(sd.getLogisticsMileage() + dealExpensesChange.getLogisticsMileage());
                } else {
                    sd.setExpensesNotSettlementMoney(sd.getExpensesNotSettlementMoney().subtract(dealExpensesChange.getExpensesMoney()));
                }
                sd.setExpensesSettlementMoney(sd.getExpensesSettlementMoney().add(dealExpensesChange.getExpensesMoney()));
                break;
            case "rejected":
                if (sd != null) {
                    sd.setExpensesMoney(sd.getExpensesMoney().subtract(dealExpensesChange.getExpensesMoney()));
                    sd.setExpensesNotSettlementMoney(sd.getExpensesNotSettlementMoney().subtract(dealExpensesChange.getExpensesMoney()));
                    sd.setLogisticsMileage(sd.getLogisticsMileage() - dealExpensesChange.getLogisticsMileage());
                }
                break;
        }
        if (sd != null && sd.getId() == null) {
            if (statisticsDriverMapper.insert(sd) == 0) {
                logger.error("===添加驾驶员报表失败");
            }
        } else {
            if (statisticsDriverMapper.updateById(sd) == 0) {
                logger.error("===更新驾驶员报表失败");
            }
        }
        logger.info("===更新驾驶员报表 ===END===");

        logger.info("===更新车辆报表【carId:{}】 ===START===", dealExpensesChange.getCarId());
        StatisticsCar sc = statisticsCarMapper.findByCarIdAndCreateDate(dealExpensesChange.getCarId(), createDate);
        switch (dealExpensesChange.getType()) {
            case "financeAudit":
                if (sc == null) {
                    sc = new StatisticsCar(dealExpensesChange.getCarId(), newDate);
                }
                sc.setExpensesMoney(sc.getExpensesMoney().add(dealExpensesChange.getExpensesMoney()));
                sc.setExpensesNotSettlementMoney(sc.getExpensesNotSettlementMoney().add(dealExpensesChange.getExpensesMoney()));
                sc.setLogisticsMileage(sc.getLogisticsMileage() + dealExpensesChange.getLogisticsMileage());
                break;
            case "complete":
                if (sc == null) {
                    sc = new StatisticsCar(dealExpensesChange.getCarId(), newDate);
                    sc.setExpensesMoney(sc.getExpensesMoney().add(dealExpensesChange.getExpensesMoney()));
                    sc.setLogisticsMileage(sc.getLogisticsMileage() + dealExpensesChange.getLogisticsMileage());
                } else {
                    sc.setExpensesNotSettlementMoney(sc.getExpensesNotSettlementMoney().subtract(dealExpensesChange.getExpensesMoney()));
                }
                sc.setExpensesSettlementMoney(sc.getExpensesSettlementMoney().add(dealExpensesChange.getExpensesMoney()));
                break;
            case "rejected":
                if (sc != null) {
                    sc.setExpensesMoney(sc.getExpensesMoney().subtract(dealExpensesChange.getExpensesMoney()));
                    sc.setExpensesNotSettlementMoney(sc.getExpensesNotSettlementMoney().subtract(dealExpensesChange.getExpensesMoney()));
                    sc.setLogisticsMileage(sc.getLogisticsMileage() - dealExpensesChange.getLogisticsMileage());
                }
                break;
        }
        if (sc != null && sc.getId() == null) {
            if (statisticsCarMapper.insert(sc) == 0) {
                logger.error("===添加车辆报表失败");
            }
        } else {
            if (statisticsCarMapper.updateById(sc) == 0) {
                logger.error("===更新车辆报表失败");
            }
        }
        logger.info("===更新车辆报表 ===END===");

        logger.info("===consumer【STATISTICS_EXPENSES_REJECTED】货运支出报表 ===END===");
    }

}
